package priv.conceit.sc.hoxton.common.util;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 文件后缀格式 检验工具类
 *
 * @author Conceit
 * @since 1.0.0, 2017/08/21
 */
public class FileSuffixUtil {

	public static final List<String> DEFAULT_UPLOAD_PICTURE_SUFFIXS = Arrays.asList("bmp", "gif", "jpeg", "png", "jpg");
	public static final List<String> DEFAULT_UPLOAD_VIDEO_SUFFIXS = Arrays.asList("mp4", "mov", "flv", "mkv", "3gp", "avi", "rm", "rmvb", "asf", "mpg");
	public static final List<String> DEFAULT_UPLOAD_TEXT_SUFFIXS = Arrays.asList("txt", "html", "htm", "asp", "jsp", "xml", "json", "properties", "md", "gitignore", "log", "java", "py", "c", "cpp", "sql", "sh", "bat", "ini");
	public static final List<String> DEFAULT_UPLOAD_COMPRESS_SUFFIXS = Arrays.asList("gz", "rar", "zip", "7z", "tar", "jar");
	public static final List<String> DEFAULT_UPLOAD_OTHER_SUFFIXS = Arrays.asList("psd", "psb");
	public static final List<String> DEFAULT_UPLOAD_PDF_SUFFIXS = Arrays.asList("pdf");
	public static final List<String> DEFAULT_UPLOAD_MUSIC_SUFFIXS = Arrays.asList("ogg", "mp3", "wma", "midi");
	public static final List<String> DEFAULT_UPLOAD_OFFICE_SUFFIXS = Arrays.asList("doc", "docx", "xls", "xlsx", "pptx", "ppt", "pptm");
	public static final List<String> DEFAULT_UPLOAD_CAD_SUFFIXS = Arrays.asList("dwg", "dxf", "dwt");

	public static final List<String> DEFAULT_PREVIEW_PICTURE_SUFFIXS = Arrays.asList("bmp", "gif", "jpeg", "png", "jpg");
	public static final List<String> DEFAULT_PREVIEW_VIDEO_SUFFIXS = Arrays.asList("mp4", "mov", "flv", "mkv", "3gp", "avi", "asf", "mpg");
	public static final List<String> DEFAULT_PREVIEW_TEXT_SUFFIXS = Arrays.asList("txt", "html", "htm", "asp", "jsp", "xml", "json", "properties", "md", "gitignore", "log", "java", "py", "c", "cpp", "sql", "sh", "bat", "ini");
	public static final List<String> DEFAULT_PREVIEW_COMPRESS_SUFFIXS = Arrays.asList("rar", "zip", "7z", "tar", "jar");
	public static final List<String> DEFAULT_PREVIEW_PDF_SUFFIXS = Arrays.asList("pdf");
	public static final List<String> DEFAULT_PREVIEW_MUSIC_SUFFIXS = Arrays.asList("ogg", "mp3", "wma", "midi");
	public static final List<String> DEFAULT_PREVIEW_OFFICE_SUFFIXS = Arrays.asList("doc", "docx", "xls", "xlsx", "pptx", "ppt", "pptm");
	public static final List<String> DEFAULT_PREVIEW_CAD_SUFFIXS = Arrays.asList("dwg", "dxf", "dwt");


	/**
	 * 目前市面上三种主流的压缩格式(以及tar(类unix),gz) 进行分类，对应不同的解压方式
	 * <p>
	 * 有别于其他平台gz为压缩格式，，归档 和压缩是两回事。gz格式只是压缩格式多文件和文件夹无法直接压缩，需要归档（tar）
	 */
	public static final List<String> SEVENZ_COMPRESS_SUFFIXS = Arrays.asList("7z");
	public static final List<String> ZIP_COMPRESS_SUFFIXS = Arrays.asList("zip", "jar");
	public static final List<String> RAR_COMPRESS_SUFFIXS = Arrays.asList("rar");
	public static final List<String> TAR_COMPRESS_SUFFIXS = Arrays.asList("tar");


	/**
	 * 检查传入的文件后缀是否为任意的合法格式
	 *
	 * @param suffixs    传入的合法文件后缀集
	 * @param suffix     文件后缀
	 * @param ignoreCase 是否忽略大小写
	 * @return boolean
	 */
	public static boolean suffixAnyMatch(List<String> suffixs, String suffix, boolean ignoreCase) {
//		if (CollectionUtils.isEmpty(suffixs) || !StringUtils.hasText(suffix)) {
//			return false;
//		}
		if (ignoreCase) {
			return suffixs.stream().anyMatch(x -> x.equalsIgnoreCase(suffix));
		} else {
			return suffixs.stream().anyMatch(x -> x.equals(suffix));
		}
	}

	/**
	 * 检查传入的文件名( 内部转化为后缀) 是否为 任意合法格式
	 *
	 * @param suffixs    传入的合法文件后缀集
	 * @param fileName   文件名称
	 * @param ignoreCase 是否忽略大小写
	 * @return boolean
	 */
	public static boolean fileNameAnyMatch(List<String> suffixs, String fileName, boolean ignoreCase) {
//		if (CollectionUtils.isEmpty(suffixs) || !StringUtils.hasText(fileName)) {
//			return false;
//		}
		String suffix = FileUtil.getFileSuffix(fileName);
		if (ignoreCase) {
			return suffixs.stream().anyMatch(x -> x.equalsIgnoreCase(suffix));
		} else {
			return suffixs.stream().anyMatch(x -> x.equals(suffix));
		}

	}

	/**
	 * 获取多个文件名 是否符合types规范,并返回不符合的fileName
	 *
	 * @param suffixs    传入的合法文件后缀集
	 * @param fileNames  传入的合法文件名称集
	 * @param ignoreCase 是否忽略大小写
	 * @return List<String>
	 * @author conceit
	 * @date 2020/6/03 22:23
	 */
	public static List<String> callNoneMatchFileName(List<String> suffixs, List<String> fileNames, boolean ignoreCase) {
//		if (CollectionUtils.isEmpty(suffixs)) {
//			return fileNames;
//		}
//		if (CollectionUtils.isEmpty(fileNames)) {
//			return Collections.EMPTY_LIST;
//		}
		List<String> result = new ArrayList<>();
		for (String filename : fileNames) {
			boolean flag = fileNameAnyMatch(suffixs, filename, ignoreCase);
			if (!flag) {
				result.add(filename);
			}
		}
		return result;
	}


	/**
	 * merge 合并多个默认的后缀名list
	 *
	 * @param suffixs 将传入的多个List 进行合并。
	 * @return java.util.List<R>
	 * @author conceit
	 * @date 2020/5/16 16:23
	 */
	public static <T> List merge(List<T>... suffixs) {
		List<T> merge = new ArrayList();
		Arrays.stream(suffixs).forEach(type -> {
			merge.addAll(type);
		});
		return merge;
	}


	/**
	 * merge 合并多个默认上传支持的文件类型后缀名list
	 *
	 * @return java.util.List<R>
	 * @author conceit
	 * @date 2020/5/16 16:23
	 */
	public static List mergeDefaultUploadSuffixs() {
		Field[] fields = FileSuffixUtil.class.getDeclaredFields();
		List<String> merge = new ArrayList<>();
		for (Field field : fields) {
			if (field.getName().startsWith("DEFAULT_UPLOAD")) {
				try {
					merge.addAll((Collection<? extends String>) field.get(null));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return merge;
	}

	/**
	 * merge 合并多个默认上传支持的文件类型后缀名list
	 *
	 * @return java.util.List<R>
	 * @author conceit
	 * @date 2020/5/16 16:23
	 */
	public static List mergeDefaultPreviewSuffixs() {
		Field[] fields = FileSuffixUtil.class.getDeclaredFields();
		List<String> merge = new ArrayList<>();
		for (Field field : fields) {
			if (field.getName().startsWith("DEFAULT_PREVIEW")) {
				try {
					merge.addAll((Collection<? extends String>) field.get(null));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return merge;
	}


}
